package adapter;

import java.util.ArrayList;
import java.util.List;

import util.ApplicationConstant;

import com.activity.MainAppActivity;
import com.activity.MapActivity;
import com.activity.R;
import com.google.android.gms.internal.co;

import dao.LoadCategory;
import entities.Category;
import entities.MenuItem;
import entities.NavigationItem;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

/**
 * @author thoai nguyen adapter tao navigate
 * 
 */
public class AdapterNavigation extends ArrayAdapter<NavigationItem> {
	private List<NavigationItem> list;
	private MainAppActivity context;
	private ArrayList<MenuItem> listMenu=new ArrayList<MenuItem>();
	public AdapterNavigation(MainAppActivity context, int textViewResourceId,
			List<NavigationItem> objects) {
		super(context, textViewResourceId, objects);
		this.list = objects;
		this.context = context;
		// TODO Auto-generated constructor stub
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		convertView = inflater.inflate(list.get(position).getLayoutId(), null);
		/*
		 * list menu
		 */
		if (list.get(position).getLayoutId() == R.layout.nav_layout2) {
			int menuName[] = {R.string.menu_home,R.string.menu_bookmark, R.string.menu_my_locate,
					R.string.menu_recent };
			int menuImg[] = {R.drawable.ic120, R.drawable.ic226, R.drawable.ic161, R.drawable.ic176 };
			listMenu = new ArrayList<MenuItem>();
			for (int i = 0; i < menuName.length; i++) {
				MenuItem item = new MenuItem(menuName[i], menuImg[i]);
				listMenu.add(item);
			}
			ListView listView = (ListView) convertView
					.findViewById(R.id.listView_menu);
			final AdapterMenu adapterMenu = new AdapterMenu(context,
					R.layout.menu_item, listMenu);
			listView.setAdapter(adapterMenu);
		
			
			listView.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> adapter, View view,
						int pos, long arg3) {
					// TODO Auto-generated method stub
					for(MenuItem item:listMenu)
						item.setSelected(false);
					listMenu.get(pos).setSelected(true);
					adapterMenu.notifyDataSetChanged();
					
					switch (pos) {
					case 0:
						context.navigateToMainFragment();
						break;
					case 1:
						ApplicationConstant.IS_CALLING_BOOKMARK=true;
						ApplicationConstant.IS_CALLING_RECENT=false;
						context.navigateToBookMarkRecent();
						break;
					case 2:
						ApplicationConstant.IS_CALLING_BOOKMARK=false;
						ApplicationConstant.IS_CALLING_RECENT=false;
						context.navigateToMyLocate();
						break;
					case 3:
						ApplicationConstant.IS_CALLING_BOOKMARK=false;
						ApplicationConstant.IS_CALLING_RECENT=true;
						context.navigateToBookMarkRecent();
						break;
					default:
						break;
					}
				}
			});
		}
		/*
		 * dia chi
		 */
		else if (list.get(position).getLayoutId() == R.layout.nav_layout1) {
			TextView textViewAddress = (TextView) convertView
					.findViewById(R.id.textView_main_address);
			textViewAddress.setTypeface(MainAppActivity.typeFaceFont);
			TextView textViewLa = (TextView) convertView
					.findViewById(R.id.textView_main_la);
			textViewLa.setTypeface(MainAppActivity.typeFaceFont);
			TextView textViewLog = (TextView) convertView
					.findViewById(R.id.textView_main_long);
			textViewAddress.setText(MainAppActivity.nowAddress);
			textViewLa.setText(String
					.valueOf(MainAppActivity.orginalLatl.latitude));
			
			textViewLog.setText(String
					.valueOf(MainAppActivity.orginalLatl.longitude));
			textViewLog.setTypeface(MainAppActivity.typeFaceFont);
			LinearLayout layout=(LinearLayout) convertView.findViewById(R.id.layout_nav1);
			layout.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Intent intent=new Intent(context,MapActivity.class);
					context.startActivity(intent);
				}
			});
		}
		return convertView;
	}
}
